﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// ClientUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class ClientUserControl : UserControl
    {
        string ip = "127.0.0.1";
        int port = 1234;
        bool isConnected = false;
        IPSocket socket;
        public ClientUserControl()
        {
            InitializeComponent();
            socket = new P_Client();
        }

        private void Open_Click(object sender, RoutedEventArgs e)
        {
            
            try
            {
                var temp = socket.OpenConnected(ip, port);
                isConnected = temp == null;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            if (isConnected)
            {
                //MessageBox.Show("打开成功");
                addMsg("打开成功");
            }
        }

        void addMsg(string m)
        {
            msg.Text += m + "\n";
            sc.ScrollToEnd();
        }

        private void Close_Click(object sender, RoutedEventArgs e)
        {
            if (isConnected) {
                if (socket.CloseConnected())
                {
                    addMsg("关闭成功");
                }
                else
                {
                    addMsg("关闭成功");
                }
            }
        }

        private void Send_Click(object sender, RoutedEventArgs e)
        {
            if (!isConnected) {
                MessageBox.Show("请先打开连接");
            }
            socket.SendData(clientMsg.Text);
        }
    }
}
